import 'package:ccshop/entites/MenuEntity.dart';
import 'package:ccshop/eventbus/EventBus.dart';
import 'package:ccshop/ui/pages/AllOrderPage.dart';
import 'package:ccshop/ui/pages/CameraPage.dart';
import 'package:ccshop/ui/pages/LoginPage.dart';
import 'package:ccshop/utils/Constants.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MineTopView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: Stack(
        children: [
          // 用户信息
          Positioned(
            child: _UserInfoView(),
          ),

          // 订单信息
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: _OrderView(),
          )
        ],
      ),
    );
  }
}

class _OrderView extends StatelessWidget {
  List<MenuEntity> _list;

  _OrderView() {
    _list = List();

    MenuEntity entity = MenuEntity("images/shop_wait_pay.png", "待付款");
    _list.add(entity);

    entity = MenuEntity("images/shop_wait_distribution.png", "待配送");
    _list.add(entity);

    entity = MenuEntity("images/shop_wait_pick.png", "待提货");
    _list.add(entity);

    entity = MenuEntity("images/shop_already_pick.png", "已提货");
    _list.add(entity);

    entity = MenuEntity("images/shop_service.png", "售后服务");
    _list.add(entity);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 110,
      margin: EdgeInsets.only(left: 10, right: 10),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
      child: Column(
        children: [
          Container(
            height: 44,
            padding: EdgeInsets.all(10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "我的订单",
                  style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.bold,
                      fontSize: 16),
                ),
                Row(
                  children: [
                    InkWell(
                      onTap: () {
                        Navigator.push(context,
                            MaterialPageRoute(builder: (context) {
                          return AllOrderPage(0);
                        }));
                      },
                      child: Text(
                        "查看全部订单",
                      ),
                    ),
                    Icon(Icons.keyboard_arrow_right)
                  ],
                )
              ],
            ),
          ),
          Divider(
            color: Color(0xFFEEEEEE),
            height: 2,
          ),
          Expanded(
            child: GridView.builder(
                itemCount: _list.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: _list.length,
                ),
                itemBuilder: (context, index) {
                  return InkWell(
                    onTap: () {
                      jumpOrderPage(context, index);
                    },
                    child: Column(
                      children: [
                        Padding(
                          padding: EdgeInsets.only(top: 10),
                        ),
                        Image.asset(
                          _list[index].menuSrc,
                          width: 20,
                          height: 20,
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 6),
                        ),
                        Text(
                          _list[index].menuName,
                          style: TextStyle(fontSize: 12),
                        )
                      ],
                    ),
                  );
                }),
          )
        ],
      ),
    );
  }

  void jumpOrderPage(BuildContext context, int index) {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return AllOrderPage(index);
    }));
  }
}

class _UserInfoView extends StatefulWidget {
  @override
  __UserInfoViewState createState() => __UserInfoViewState();
}

class __UserInfoViewState extends State<_UserInfoView> {
  String _account;

  void getAccount() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    setState(() {
      _account = preferences.getString(Constants.USER_INFO_ACCOUNT);
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getAccount();

    bus.on(Constants.EVENT_LOGIN_ACCOUNT, (arg) {
      print('arg = $arg');
      setState(() {
        _account = arg;
      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    bus.off(Constants.EVENT_LOGIN_ACCOUNT);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 160,
      color: Colors.red,
      alignment: AlignmentDirectional.topStart,
      child: InkWell(
        onTap: () async {
          Navigator.push(context, MaterialPageRoute(builder: (context) {
            return _account == null ? LoginPage() : CameraPage();
          }));
        },
        child: Row(
          children: [
            Padding(
              padding: EdgeInsets.only(left: 20),
            ),
            ClipOval(
              child: Image.asset(
                "images/shop_default_avatar.png",
                width: 60,
                height: 60,
                color: Colors.white,
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 10),
            ),
            Text(
              _account == null ? "点击登录账户" : _account,
              style: TextStyle(color: Colors.white, fontSize: 16),
            )
          ],
        ),
      ),
    );
  }
}
